package com.lyfx.ly.service.impl;

import com.alibaba.fastjson.JSON;
import com.lyfx.ly.entity.ClassCourseCategoryEntity;
import com.lyfx.ly.entity.StudyStepEntity;
import com.lyfx.ly.service.AttrGroupService;
import com.lyfx.ly.service.ClassCourseCategoryService;
import com.lyfx.ly.service.StudyStepService;
import com.lyfx.ly.vo.*;
import org.apache.commons.lang.StringUtils;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyfx.common.utils.PageUtils;
import com.lyfx.common.utils.Query;

import com.lyfx.ly.dao.ClassCourseDao;
import com.lyfx.ly.entity.ClassCourseEntity;
import com.lyfx.ly.service.ClassCourseService;
import org.springframework.transaction.annotation.Transactional;


@Service("classCourseService")
public class ClassCourseServiceImpl extends ServiceImpl<ClassCourseDao, ClassCourseEntity> implements ClassCourseService {
    @Autowired
    ClassCourseCategoryService classCourseCategoryService;
    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    StudyStepService studyStepService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        QueryWrapper<ClassCourseEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.eq("course_id", key).or().like("name", key);
        }

        IPage<ClassCourseEntity> page = this.page(
                new Query<ClassCourseEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 保证冗余字段的一致性
     */
    @Override
    @Transactional
    public void updateDetail(ClassCourseEntity classCourse) {
        //保证自己表一致
        this.updateById(classCourse);
        if(!StringUtils.isEmpty(classCourse.getName())){
            //同步更新关联表的数据
            classCourseCategoryService.updateCourse(classCourse.getCourseId(),classCourse.getName());

            // TODO 更新其他关联表
        }
    }

    /**
     * 获取详情等基本信息
     */
    @Override
    public ClassItemVo itemClass(Long classId) {
        ClassItemVo classItemVo = new ClassItemVo();
        //1 课程基本信息
        ClassCourseEntity byId = this.getById(classId);
        BeanUtils.copyProperties(byId, classItemVo);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String sDate = simpleDateFormat.format(byId.getGmtModified());
        classItemVo.setTime(sDate);

        ClassCourseCategoryEntity courseId = classCourseCategoryService.getOne(new QueryWrapper<ClassCourseCategoryEntity>().eq("course_id", classId));
        //2 课程属性信息
        List<ItemAttrGroupVo> groupVoList = attrGroupService.getAttrGroupWithAttrsByClassId(classId,courseId.getCategoryId());
        classItemVo.setAttrGroupVos(groupVoList);
        //3 课程步骤信息
        List<StudyStepVo> studyStepVos = studyStepService.listWithTree(classId);
        List<DtreeStepVo> dtreeStepVos = stepWithDtreeVo(studyStepVos);
        classItemVo.setStepStr(JSON.toJSONString(dtreeStepVos));
        return classItemVo;
    }

    /***
     * 转换成dtree数据
     */
    private List<DtreeStepVo> stepWithDtreeVo(List<StudyStepVo> studyStepVos) {
        List<DtreeStepVo> collect = studyStepVos.stream().map(item -> {
            DtreeStepVo dtreeStepVo = new DtreeStepVo();
            dtreeStepVo.setId(item.getStepId());
            dtreeStepVo.setParentId(item.getParentSid());
            dtreeStepVo.setTitle(item.getStepName());
            if (item.getChildren() != null && item.getChildren().size() > 0) {
                dtreeStepVo.setLast(false);
                dtreeStepVo.setChildren(stepWithDtreeVo(item.getChildren()));
            } else {
                dtreeStepVo.setLast(true);
            }
            return dtreeStepVo;
        }).collect(Collectors.toList());
        return collect;
    }


}